package com.study.scala

import scala.collection.parallel.{ForkJoinTaskSupport, ParSeq}

/**
 * @program: spark2.3.2
 * @author: Zhoujian
 * @date: 2022-07-01 16:02
 * @version: 1.0
 * @description: ${description}
 * */
object ThreadPollTest {

	implicit class PoolParallelCollection[T](val col: Seq[T]) {
		def parN(poolSize: Int): ParSeq[T] = {
			val colPar: ParSeq[T] = col.par
			val taskSupport = new ForkJoinTaskSupport(new scala.concurrent.forkjoin.ForkJoinPool(poolSize))
			colPar.tasksupport = taskSupport
			colPar
		}
	}

	def main(args: Array[String]): Unit = {
		val hourPartitions = Seq("/hour={00,01,02}", "/hour={03,04,05}", "/hour={06,07,08}", "/hour={09,10}", "/hour={11,12}",
			"/hour={13,14}", "/hour={15,16}", "/hour={17,18}", "/hour={19,20}", "/hour={21,22,23}")

		hourPartitions.parN(5).foreach(hourPartition =>{
			val str = hourPartition.substring(7, 9)
			println(Thread.currentThread().getName + " -------- " + str)
		})

		hourPartitions.par.foreach(hourPartition =>{
			val str = hourPartition.substring(7, 9)
			println(Thread.currentThread().getName + " -------- " + str)
		})
	}

}
